<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="订单号" prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号" prop="phonenumber">
                <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="用户名" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="查询时间">
                <el-date-picker v-model="dateRange" style="width: 240px" value-format="YYYY-MM-DD" type="daterange"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="退款状态" prop="refundStatus">
                <el-select @change="getList" v-model="queryParams.refundStatus" placeholder="请选择退款状态" clearable>
                    <el-option v-for="dict in refund_status" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="infoList">
            <el-table-column label="订单号" align="center" prop="orderNo" :show-overflow-tooltip="true" />
            <el-table-column label="用户名" align="center" prop="userName" />
            <el-table-column label="手机号" align="center" prop="phonenumber" />
            <el-table-column label="退款状态" align="center" prop="refundStatus">
                <template #default="scope">
                    <dict-tag :options="refund_status" :value="scope.row.refundStatus" />
                </template>
            </el-table-column>
            <el-table-column label="原订单金额" align="center" prop="totalAmount">
                <template #default="scope">
                    <span>￥{{ scope.row.totalAmount / 100 || 0 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单支付金额" align="center" prop="payerTotal">
                <template #default="scope">
                    <span>￥{{ scope.row.payerTotal / 100 || 0 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="退款金额" align="center" prop="refundAmount">
                <template #default="scope">
                    <span>￥{{ scope.row.refundAmount / 100 || 0 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="实际退款金额" align="center" prop="payerRefund">
                <template #default="scope">
                    <span>￥{{ scope.row.payerRefund / 100 || 0 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="实际退款积分" align="center" prop="payerRefundCoins">
                <template #default="scope">
                    <span>{{ scope.row.payerRefundCoins || 0 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="退款创建时间" align="center" prop="createTime" width="180">
                <template #default="scope">
                    <span v-if="scope.row.createTime">
                        {{ parseTime(scope.row.createTime) }}
                    </span>
                    <span v-else>-</span>
                </template>
            </el-table-column>
            <el-table-column label="退款原因" align="center" prop="reason" />
            <el-table-column label="退款到账时间" align="center" prop="endTime" width="180">
                <template #default="scope">
                    <span v-if="scope.row.successTime">
                        {{ parseTime(scope.row.successTime) }}
                    </span>
                    <span v-else>-</span>
                </template>
            </el-table-column>
            <el-table-column label="用户收款账户" align="center" prop="refundAccount" />
            <el-table-column label="操作" align="center">
                <template #default="scope">
                    <el-button type="primary" v-if="scope.row.refundStatus == 'WAIT'" @click="handleAudit(scope.row)">退款审核
                    </el-button>
                    <span v-else>-</span>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>

<script setup name="OrderRefund">
import { listOrder, auditRefund } from "@/api/trade/refund";
import { showConfirmDialog } from 'vant';

const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();
const { refund_status } = proxy.useDict("refund_status");

const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const infoList = ref([])
const dateRange = ref("")

const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    orderNo: undefined,
    phonenumber: undefined,
    userName: undefined,
    refundStatus: undefined,
    endTime: undefined,
    beginTime: undefined,
})
// 查询订单信息列表
function getList() {
    loading.value = true;
    if (queryParams.value.chargeMode == "") queryParams.value.chargeMode = null;
    if (queryParams.value.chargeStatus == "")
        queryParams.value.chargeStatus = null;
    if (queryParams.value.chargeType == "") queryParams.value.chargeType = null;
    if (queryParams.value.payStatus == "") queryParams.value.payStatus = null;
    listOrder(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
        infoList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}
// 搜索按钮操作
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
// 重置按钮操作
function resetQuery() {
    proxy.resetForm("queryForm");
    handleQuery();
}
// 退款审核
function handleAudit(row) {
    showConfirmDialog({
        title: "退款操作",
        message: "此条退款订单是否同意？",
        confirmButtonText: "同意",
        cancelButtonText: "拒绝",
    })
        .then(() => {
            // on confirm
            auditRefund({
                refundNo: row.refundNo,
                state: 1,
            }).then((response) => {
                proxy.$modal.notifySuccess("审核通过");
                getList();
            });
        })
        .catch(() => {
            // on cancel
            auditRefund({
                refundNo: row.refundNo,
                state: 0,
            }).then((response) => {
                proxy.$modal.notifySuccess("审核拒绝成功");
                getList();
            });
        });
}

const beginTime = route.query.beginTime;
const endTime = route.query.endTime;
if (beginTime != undefined && endTime != undefined) {
    dateRange.value = [
        decodeURIComponent(beginTime),
        decodeURIComponent(endTime),
    ];
}
getList();

</script>
